package servlet.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import pojo.Book2;
import pojo.Order;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/orderitem")
public class OrderItem extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应对象类型
        resp.setContentType("application/json;charset=utf-8");
        // 从请求中获取参数
        String orderId = req.getParameter("orderId");
        OrderService orderService = new OrderService();
        List<pojo.OrderItem> orderItem = orderService.selectOrderItem(orderId);
        List<Book2> book2s = new ArrayList<>();
        List<Integer> numberList = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        System.out.println("开始前："+LocalDateTime.now());
        for (pojo.OrderItem item : orderItem) {
//            Book2 book2 = orderService.selectBookItem(Long.parseLong(item.getBookId()));
            Book2 book2 = new Book2();
            book2.setName(item.getBookName());
            book2.setPrice(item.getPrice());
            numberList.add(item.getNumber());
            book2s.add(book2);
        }
        System.out.println("开始后："+LocalDateTime.now());
        jsonObject.put("book",book2s);
        jsonObject.put("number",numberList);
//        orderItem.getBookId()
        String jsonString = JSON.toJSONString(jsonObject);
        PrintWriter writer = resp.getWriter();
        writer.write(jsonString);
    }
}
